/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginDialog.java
 *
 * Created on Jun 21, 2011, 8:56:23 PM
 */
package ua.pp.bizon.yandex.nb;

import ua.pp.bizon.yandex.api.IJobExecutor;
import ua.pp.bizon.yandex.api.ILogin;
import ua.pp.bizon.yandex.api.IUserData;
import ua.pp.bizon.yandex.api.IUserDataManager;
import ua.pp.bizon.yandex.api.Job;
import ua.pp.bizon.yandex.api.ObjectLookup;
import ua.pp.bizon.yandex.api.impl.GetToken;

import javax.swing.JOptionPane;

/**
 * @deprecated should run explorer instead
 * @author Bizon
 */
public class LoginDialog extends javax.swing.JDialog implements ILogin {

	private static final long serialVersionUID = -4852800492702647819L;

	/** Creates new form LoginDialog */
	public LoginDialog(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
		IUserData data = ObjectLookup.lookup(IUserDataManager.class).get(false);
		if (data != null) {
			loginTextField.setText(data.getUsername());
			tokenTextField.setText(data.getToken());
		}
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		loginLabel = new javax.swing.JLabel();
		loginTextField = new javax.swing.JTextField();
		passwordLabel = new javax.swing.JLabel();
		passwordTextField = new javax.swing.JPasswordField();
		tokenLabel = new javax.swing.JLabel();
		tokenButton = new javax.swing.JButton();
		jCheckBox1 = new javax.swing.JCheckBox();
		okButton = new javax.swing.JButton();
		cancelButton = new javax.swing.JButton();
		tokenTextField = new javax.swing.JTextField();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("ua/pp/bizon/yandex/nb/Bundle"); // NOI18N
		loginLabel.setText(bundle.getString("LoginDialog.loginLabel.text")); // NOI18N

		loginTextField.setText(bundle.getString("LoginDialog.loginTextField.text")); // NOI18N
		loginTextField.setToolTipText(bundle.getString("LoginDialog.loginTextField.toolTipText")); // NOI18N
		loginTextField.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

		passwordLabel.setText(bundle.getString("LoginDialog.passwordLabel.text")); // NOI18N

		tokenLabel.setText(bundle.getString("LoginDialog.tokenLabel.text")); // NOI18N

		tokenButton.setText(bundle.getString("LoginDialog.tokenButton.text")); // NOI18N
		tokenButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				tokenButtonActionPerformed(evt);
			}
		});

		jCheckBox1.setText(bundle.getString("LoginDialog.jCheckBox1.text")); // NOI18N

		okButton.setText(bundle.getString("LoginDialog.okButton.text")); // NOI18N
		okButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				okButtonActionPerformed(evt);
			}
		});

		cancelButton.setText(bundle.getString("LoginDialog.cancelButton.text")); // NOI18N
		cancelButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cancelButtonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(loginLabel).addComponent(tokenLabel)
										.addComponent(passwordLabel))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addComponent(tokenTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
										.addComponent(loginTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 292,
												Short.MAX_VALUE).addComponent(passwordTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
										.addGroup(
												layout.createSequentialGroup().addComponent(okButton)
														.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(cancelButton))
										.addComponent(jCheckBox1).addComponent(tokenButton)).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(loginLabel)
										.addComponent(cancelButton)
										.addComponent(okButton)
										.addComponent(loginTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(tokenButton)
										.addComponent(passwordLabel)
										.addComponent(passwordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
						.addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(tokenTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(tokenLabel).addComponent(jCheckBox1)).addContainerGap()));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_okButtonActionPerformed
		ObjectLookup.lookup(IUserDataManager.class).relogin(loginTextField.getText(), tokenTextField.getText());
		if (jCheckBox1.isSelected()) {
			ObjectLookup.lookup(IUserDataManager.class).save();
		}
		setVisible(false);
	}// GEN-LAST:event_okButtonActionPerformed

	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_cancelButtonActionPerformed
		setVisible(false);
	}// GEN-LAST:event_cancelButtonActionPerformed

	private void tokenButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_tokenButtonActionPerformed
		try {
			String token = new GetToken().get(loginTextField.getText(), new String(passwordTextField.getPassword()));
			tokenTextField.setText(token);
		} catch (Exception e) {
			System.err.println(e);
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e, "error", JOptionPane.ERROR_MESSAGE);
		}
	}// GEN-LAST:event_tokenButtonActionPerformed

	/*
	 * (non-Javadoc)
	 * 
	 * @see ua.pp.bizon.yandex.nb.ILogin#setLogin(java.lang.String)
	 */
	@Override
	public void setLogin(String string) {
		loginTextField.setText(string);
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton cancelButton;
	private javax.swing.JCheckBox jCheckBox1;
	private javax.swing.JLabel loginLabel;
	private javax.swing.JTextField loginTextField;
	private javax.swing.JButton okButton;
	private javax.swing.JLabel passwordLabel;
	private javax.swing.JPasswordField passwordTextField;
	private javax.swing.JButton tokenButton;
	private javax.swing.JLabel tokenLabel;
	private javax.swing.JTextField tokenTextField;

	/*
	 * (non-Javadoc)
	 * 
	 * @see ua.pp.bizon.yandex.nb.ILogin#waitUserData()
	 */
	@Override
	public void waitUserData() {
		setVisible(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ua.pp.bizon.yandex.nb.ILogin#unblockingWaitUserData()
	 */
	@Override
	public void unblockingWaitUserData() {
		ObjectLookup.lookup(IJobExecutor.class).execInSwing(new Job() {
			@Override
			public void run() {
				setVisible(true);
			}
		}, false);

	}
	// End of variables declaration//GEN-END:variables

}
